package de.hs_mannheim.visualscheduler;

import java.awt.Container;
import java.awt.GridLayout;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.pushingpixels.substance.api.SubstanceConstants.ImageWatermarkKind;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.MistAquaSkin;
import org.pushingpixels.substance.api.watermark.SubstanceImageWatermark;
import de.hs_mannheim.visualscheduler.gui.MainGUI;

/**
 * The Class Main is the Entry-Point of the VisualScheduler application.
 * The Class is startable as an Applet or standalone-application 
 */
public class Main extends JApplet {
	/**
	 * The Class VisualSchedulerSkin is the default Skin for VisualScheduler.
	 */
	public static class VisualSchedulerSkin extends MistAquaSkin {
		public VisualSchedulerSkin() {
			watermark = new SubstanceImageWatermark(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("MetalGalvanized0022_7_thumbhuge.jpg"));
			((SubstanceImageWatermark) watermark).setKind(ImageWatermarkKind.SCREEN_TILE);
			((SubstanceImageWatermark) watermark).setOpacity(0.175f);
		}
	}

	private static final long	serialVersionUID	= -9032881939194354826L;

	public static void main(final String[] args) throws InterruptedException, InvocationTargetException {

		// Entry Point if started as Standalone
		SwingUtilities.invokeAndWait(new Runnable() {
			@Override
			public void run() {

				// Construct the main Frame
				JFrame.setDefaultLookAndFeelDecorated(true);
				final JFrame frame = new JFrame("VisualScheduler");
				try {
					frame.setIconImage(ImageIO.read(Thread.currentThread().getContextClassLoader()
							.getResource("Logo16x16.png")));
				} catch (final IOException e) {
					e.printStackTrace();
				}
				init(frame);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
				frame.pack();
				frame.createBufferStrategy(2);
			}
		});

	}

	/**
	 * Builds and initailizes the main-gui block into the container specified.
	 * The Contianer is either a Applet or a Frame.
	 * 
	 * @param container
	 *            to build the gui into
	 */
	static void init(final Container container) {
		final Runnable job = new Runnable() {
			@Override
			public void run() {
				// Apply fix for Substance
				try {
					System.setProperty("sun.awt.noerasebackground", "true");
				} catch (final Exception e) {
				}
				UIManager.put(SubstanceLookAndFeel.WATERMARK_VISIBLE, Boolean.TRUE);

				// Use Substance as Look And Feel
				SubstanceLookAndFeel.setSkin(new VisualSchedulerSkin());

				container.add(new MainGUI());
				SwingUtilities.updateComponentTreeUI(container);
			}
		};
		try {
			if (!SwingUtilities.isEventDispatchThread()) {
				SwingUtilities.invokeAndWait(job);
			} else {
				job.run();
			}
		} catch (final InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (final InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Main() {
		// Applet entry-point
		setLayout(new GridLayout(1, 1));
		Main.init(this);
	}
}